// pagesFilterDicSelect/pagesFilterDicSelect.js
// https: //github.com/wechat-miniprogram/computed
const computedBehavior = require('miniprogram-computed').behavior
import {
  convertToTree,
  deptAndGroupAndUserToConvert,
  deptAndTeamToConvert,
  deptToConvert,
  flattenTree
} from '../../../../utils/util.js';
const app = getApp()
Component({
  behaviors: [computedBehavior, ],
  properties: {
    visible: {
      type: Boolean,
      value: false
    },
    value: {
      type: String,
      value: ''
    },
    multiple: {
      type: Boolean,
      value: true
    },
    choseType: {
      type: String,
      //   selectDept | selectDeptTeamUser | selectTeamUser
      value: 'selectDept'
    }

  },
  /**
   * 页面的初始数据
   */
  data: {
    dataList: [],
    sourceData: [],
    copyViewListData: [],
    viewListData: [],
    echoValue: []
  },
  watch: {
    'visible': function (v) {
      console.log('wathc', v);
      if (v) {
        this.onVisibleChange()

      }
    }
  },
  lifetimes: {

  },

  methods: {
    onVisibleChange() {
      let sourceData = []
      let copyViewListData = []
      let viewListData = []
      // choseType: selectDept | selectDeptTeamUser | selectTeamUser
      if (this.data.choseType === 'selectDept') {
        sourceData = deptToConvert(app.globalData.deptList)
        const treeList = convertToTree(sourceData, {
          id: 'deptId',
          parentId: 'parentId',
          name: 'deptName'
        })
        copyViewListData = treeList
        viewListData = treeList
      } else if (this.data.choseType === 'selectDeptTeamUser') {
        const list = deptAndGroupAndUserToConvert(app.globalData.deptTeamUserList)
        copyViewListData = list
        viewListData = list
        sourceData = flattenTree(list)
      } else {

        const list = deptAndTeamToConvert(app.globalData.deptTeamUserList)
        copyViewListData = list
        viewListData = list
        sourceData = flattenTree(list)
      }
      this.setData({
        multiple: this.data.multiple,
        sourceData: sourceData,
        copyViewListData: copyViewListData,
        viewListData: viewListData,
        echoValue: [this.data.value]
      })
    },
    // 确认
    onConfirm({
      detail
    }) {
      this.setData({
        visible: false,
      })
      this.triggerEvent('onConfirmDepSelectPopup', {
        value: detail.value[0].value,
        valueLabel: detail.value[0].name,
        other: detail.value[0]
      })

    },
    // 取消
    onCancel() {
      this.setData({
        visible: false
      })
    },
  }

})